package com.xindaoapp.framework.activity;

import java.text.DecimalFormat;
import java.util.LinkedList;
import java.util.List;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.squareup.picasso.Picasso;
import com.xindaoapp.framework.R;
import com.xindaoapp.framework.api.NewApiImpl;
import com.xindaoapp.framework.bean.Good;
import com.xindaoapp.framework.bean.SerachGood;
import com.xindaoapp.framework.utils.IRequest;

public class NewsActivity extends BaseActivity {
	private PullToRefreshListView pull;
	private ListView listview;
	private RelativeLayout no_result;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_refresh);
		setTitle("新品");
		initUI();
		initNews(page);
	}
	private void initUI() {
		no_result=(RelativeLayout) findViewById(R.id.no_result);
		pull=(PullToRefreshListView) findViewById(R.id.pull);
		impl = NewApiImpl.getInstance();
		listview=pull.getRefreshableView();
		pull.setMode(Mode.DISABLED);
		pull.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
			@Override
			public void onLastItemVisible() {
				if(total==goods.size()){
					Toast.makeText(getApplicationContext(), "没有更多数据了", Toast.LENGTH_SHORT).show();
					pull.onRefreshComplete();
				}else{
					initNews(++page);
				}
			}
		});
		pull.getRefreshableView().setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				try {
					Intent intent = new Intent(NewsActivity.this, ProductDetailActivity1.class);
					intent.putExtra("productId", (goods.get(position-1).goods_id));
					startActivity(intent);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	private int total;
	private NewApiImpl impl;
	private int page = 1;
	private final LinkedList<Good> goods = new LinkedList<Good>();
	private GoodSerachAdapter adapter;
	private void initNews(int page1) {
		impl.getNewGoods(page1, 10, new IRequest<String>() {
			@Override
			public void request(String t) {
				try {
					SerachGood parse = JSON.parseObject(t, SerachGood.class);
					total=Integer.parseInt(parse.data.count);
					if(total<=10){
						pull.setMode(Mode.DISABLED);
					}
					if (parse.getStatus().equals("1")) {
						if (Integer.parseInt(parse.data.count) > 0) {
							List<Good> info = parse.data.info;
							goods.addAll(info);
							if (adapter == null) {
								adapter = new GoodSerachAdapter(goods);
								listview.setAdapter(adapter);
							} else {
								adapter.notifyDataSetChanged();
							}
						}
					} else {

					}
				} catch (Exception e) {
					pull.setVisibility(View.GONE);
					no_result.setVisibility(View.VISIBLE);
					e.printStackTrace();
				}
				pull.onRefreshComplete();
			}
		});
	}

	private class GoodSerachAdapter extends BaseAdapter {
		private final List<Good> goods2;

		public GoodSerachAdapter(List<Good> goods2) {
			super();
			this.goods2 = goods2;
		}

		@Override
		public int getCount() {
			return goods2.size();
		}

		@Override
		public Object getItem(int position) {
			return goods2.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = View.inflate(NewsActivity.this, R.layout.good_common_item, null);
				holder = new ViewHolder();
				holder.good_pic = (ImageView) convertView.findViewById(R.id.good_pic);
				holder.good_name = (TextView) convertView.findViewById(R.id.good_name);
				holder.good_shop_price = (TextView) convertView.findViewById(R.id.good_shop_price);
				holder.good_market_price = (TextView) convertView.findViewById(R.id.good_market_price);
				holder.good_sell = (TextView) convertView.findViewById(R.id.good_sell);
				holder.good_surplus = (TextView) convertView.findViewById(R.id.good_surplus);
				holder.goods_comment = (TextView) convertView.findViewById(R.id.goods_comment);
				holder.good_dazhe = (TextView) convertView.findViewById(R.id.good_dazhe);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
//			holder.good_name.setText(goods2.get(position).goods_name);
			holder.good_name.setText(goods2.get(position).goods_name);
			holder.good_shop_price.setText(formatPrice(Float.valueOf(goods2.get(position).shop_price)));
			holder.good_market_price.setText("￥"
					+ formatPrice(Float.valueOf(goods2.get(position).market_price)));
			holder.good_market_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
			holder.good_sell.setText(goods2.get(position).sold_number);
			holder.good_surplus.setText(goods2.get(position).goods_number);
			holder.goods_comment.setText(goods2.get(position).comment_percent);
			Picasso.with(NewsActivity.this).load(goods2.get(position).goods_thumb).into(holder.good_pic);
			float a = (Float.valueOf(goods2.get(position).shop_price) / Float
					.valueOf(goods2.get(position).market_price)) * 10;
			holder.good_dazhe.setText(formatDaZhe(a) + "折");
			return convertView;
		}
	}

	private String formatDaZhe(Float a) {
		DecimalFormat df = new DecimalFormat("0.0");
		return df.format(a);
	}

	private String formatPrice(Float a) {
		DecimalFormat df = new DecimalFormat("0.00");
		return df.format(a);
	}

	public class ViewHolder {
		ImageView good_pic;
		TextView good_name;
		TextView good_shop_price;
		TextView good_market_price;
		TextView good_sell;
		TextView good_surplus;
		TextView goods_comment;
		TextView good_dazhe;
	}

}
